/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.general.update;


import org.mybeans.Sesion;
import org.general.utilidades.Encriptador; 
import java.io.File;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author avbravo
 */
public class XMLServidorReader {

    Sesion sesion;
    Encriptador encrypter;
    private String servidor;
    private String ip;
    String database;
    String puerto;
    String user;
    String password;
    String java_home;
    String mysql_home;
//Encriptador encrypter ;

    public XMLServidorReader() {
        sesion = new Sesion();
        encrypter = new Encriptador();

    }

    public String getMysql_home() {
        return mysql_home;
    }

    public void setMysql_home(String mysql_home) {
        this.mysql_home = mysql_home;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getJava_home() {
        return java_home;
    }

    public void setJava_home(String java_home) {
        this.java_home = java_home;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getServidor() {
        return servidor;
    }

    public void setServidor(String servidor) {
        this.servidor = servidor;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getPuerto() {
        return puerto;
    }

    public void setPuerto(String puerto) {
        this.puerto = puerto;
    }

    public void Leer() {
        try {
            boolean exists = false;
            exists = (new File("servidor.xml")).exists();
            if (exists == false) {
                //no existe voy a crearlo
                XMLServidorCrear xMLServidorCrear = new XMLServidorCrear();
                xMLServidorCrear.setServidor(sesion.getServer());
                xMLServidorCrear.setDatabase(sesion.getDatabaseDB());
                xMLServidorCrear.setPuerto(sesion.getPuertoDB());
                xMLServidorCrear.setUser(sesion.getUserDB());
                password = encrypter.encrypt(sesion.getPasswordDB());
                xMLServidorCrear.setPassword(password);
                xMLServidorCrear.setJava_home(sesion.getJava_home());
                xMLServidorCrear.setMysql_home(sesion.getMysql_home());
                xMLServidorCrear.Crear();
            }

            File file = new File("servidor.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("hosting");


            for (int s = 0; s < nodeLst.getLength(); s++) {

                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element fstElmnt = (Element) fstNode;

                    NodeList nodeElemento = fstElmnt.getElementsByTagName("ip");
                    Element elementoUsername = (Element) nodeElemento.item(0);
                    NodeList listaUsername = elementoUsername.getChildNodes();
                    //System.out.println("clase : " + ((Node) listaUsername.item(0)).getNodeValue());
                    servidor = ((Node) listaUsername.item(0)).getNodeValue();
                    if (servidor == null) {
                        servidor = "";
                    }

                    nodeElemento = fstElmnt.getElementsByTagName("user");
                    elementoUsername = (Element) nodeElemento.item(0);
                    listaUsername = elementoUsername.getChildNodes();
                    user = ((Node) listaUsername.item(0)).getNodeValue();
                    if (user == null) {
                        user = "";
                    }

                    nodeElemento = fstElmnt.getElementsByTagName("password");
                    elementoUsername = (Element) nodeElemento.item(0);
                    listaUsername = elementoUsername.getChildNodes();
                    password = ((Node) listaUsername.item(0)).getNodeValue();
                    if (password == null) {
                        password = "";
                    }
                    //Descencriptar el password
                    password = encrypter.decrypt(password);

                    nodeElemento = fstElmnt.getElementsByTagName("java_home");
                    elementoUsername = (Element) nodeElemento.item(0);
                    listaUsername = elementoUsername.getChildNodes();
                    java_home = ((Node) listaUsername.item(0)).getNodeValue();
                    if (java_home == null) {
                        java_home = "";
                    }

                    nodeElemento = fstElmnt.getElementsByTagName("mysql_home");
                    elementoUsername = (Element) nodeElemento.item(0);
                    listaUsername = elementoUsername.getChildNodes();
                    mysql_home = ((Node) listaUsername.item(0)).getNodeValue();
                    if (mysql_home == null) {
                        mysql_home = "";
                    }

                }

            }

        } catch (Exception ex) {
            if (ex.getMessage() == null) {
              JOptionPane.showMessageDialog(null, "Clase:XMLServidorReader.java\nMetodo:Leer()\nSugerencia elimine los archivos .xml\nError " +ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
             File file2 = new File("servidor.xml");
              file2.delete();
            } else {
                JOptionPane.showMessageDialog(null, "Clase:XMLServidorReader.java\nMetodo:Leer())\nError: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            servidor = "localhost";
        }

    }
}
